const wxHttpHandler = require('../../utils/wxHttpHandler.js');
const api = require('../../APIConfig/apiConfig.js');
var dateTimePicker = require('../../utils/dateTimePicker.js');
var app = getApp();

Page({
  data: {
    datas: [],
    vehicleName: '辽A60UZ4', //查车牌号
    waybillNumber: '4a6f15f80fe74f11a2f9a242c29bp852', //任务号
    startTime: '开始时间',
    endTime: '结束时间',
    dateTimeArray1: null,
    dateTime1: null,
    dateTime2: null,
    startYear: 2020,
    endYear: 2030,
  },

  onLoad: function (options) {
    let that = this;

    // 获取完整的年月日 时分秒，以及默认显示的数组
    var obj = dateTimePicker.dateTimePicker(this.data.startYear, this.data.endYear);
    var obj1 = dateTimePicker.dateTimePicker(this.data.startYear, this.data.endYear);
    // 精确到分的处理，将数组的秒去掉
    //var lastArray = obj1.dateTimeArray.pop();
    //var lastTime = obj1.dateTime.pop();

    this.setData({
      dateTimeArray1: obj1.dateTimeArray,
      dateTime1: obj1.dateTime
    });
  },

  onShow: function () {},

  // 选择日期
  changeDateTime1(e) {
    console.log(e.detail.value)
    this.setData({
      dateTime1: e.detail.value,
    });
    this.setData({
      startTime: this.data.dateTimeArray1[0][this.data.dateTime1[0]] +
        '-' + this.data.dateTimeArray1[1][this.data.dateTime1[1]] +
        '-' + this.data.dateTimeArray1[2][this.data.dateTime1[2]] +
        ' ' + this.data.dateTimeArray1[3][this.data.dateTime1[3]] +
        ':' + this.data.dateTimeArray1[4][this.data.dateTime1[4]]
    })
  },


  // 选择时间
  changeDateTimeColumn1(e) {
    var arr = this.data.dateTime1,
      dateArr = this.data.dateTimeArray1;

    arr[e.detail.column] = e.detail.value;
    dateArr[2] = dateTimePicker.getMonthDay(dateArr[0][arr[0]], dateArr[1][arr[1]]);

    this.setData({
      dateTimeArray1: dateArr,
      dateTime1: arr
    });
  },

  // 选择日期
  changeDateTime2(e) {
    this.setData({
      dateTime2: e.detail.value
    });

    this.setData({
      endTime: this.data.dateTimeArray1[0][this.data.dateTime2[0]] +
        '-' + this.data.dateTimeArray1[1][this.data.dateTime2[1]] +
        '-' + this.data.dateTimeArray1[2][this.data.dateTime2[2]] +
        ' ' + this.data.dateTimeArray1[3][this.data.dateTime2[3]] +
        ':' + this.data.dateTimeArray1[4][this.data.dateTime2[4]]
    })
  },


  // 车牌号输入
  registerNoInput: function (e) {
    this.setData({
      vehicleName: e.detail.value
    })
  },


  toDetails: function (e) {
    let item = e.currentTarget.dataset.a
    wx.navigateTo({
      url: '../historyTemControlDetails/historyTemControlDetails?vehicleId=' + item.vehicleId + '&terminalId=' + item.terminalId + '&startTime=' + item.startTime +'&endTime=' + item.endTime,
    })
  },


  // 查询
  toQuery: function () {
    this.getHistoryVehicleTemperatureMonitorList()
  },

  //数据列表
  getHistoryVehicleTemperatureMonitorList: function () {
    let data = {
      vehicleName: this.data.vehicleName,
      waybillNumber: this.data.waybillNumber,
      startTime: this.data.startTime == '开始时间' ? null : this.data.startTime,
      endTime: this.data.endTime == '结束时间' ? null : this.data.endTime
    }
    const that = this
    wx.showLoading()
    wxHttpHandler({
      type: 'get',
      path: api.getHistoryVehicleTemperatureMonitorList + wx.getStorageSync('id'),
      data: data,
      fn: function (res) {
        wx.hideLoading()
        if (res.code == 0) {
          if (res.data != null && res.data.length > 0) {
            that.setData({
              datas: res.data
            })

          }
        } else {
          wx.showToast({
            title: res.desc,
            icon: 'none',
            duration: 1500,
            mask: true
          });
        }
      }

    })
  },
})